<?php
/**
 * Umpirsky Framework.
 *
 * LICENCE
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    * Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *    * Neither the name of the <ORGANIZATION> nor the names of its contributors
 *      may be used to endorse or promote products derived from this software without
 *      specific prior written permission.
 *
 *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *    IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 *    OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @category   Umpirsky
 * @package    Umpirsky_Service
 * @subpackage Trim
 * @copyright  Copyright (c) 2008-2009 umpirsky (http://www.umpirsky.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @author Saša Stamenković <umpirsky@gmail.com>
 */

/**
 * Service for URL shortening.
 *
 * @category   Umpirsky
 * @package    Umpirsky_Service
 * @subpackage Trim
 * @copyright  Copyright (c) 2008-2009 umpirsky (http://www.umpirsky.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Umpirsky_Service_Trim extends Zend_Rest_Client {
	/**
     * Whether or not authorization has been initialized for the current user.
     * @var bool
     */
    protected $authInitialized = false;

    /**
     * @var Zend_HttpcookieJar
     */
    protected $cookieJar;

 	/**
     * Username
     * @var string
     */
    protected $username;

    /**
     * Password
     * @var string
     */
    protected $password;

	/**
     * Constructor
     *
     * @param  string $username
     * @param  string $password
     */
	function __construct($username = null, $password = null) {
		$this->setUsername($username);
        $this->setPassword($password);
        $this->setUri('http://api.tr.im');

        $client = self::getHttpClient();
        $client->setHeaders('Accept-Charset', 'ISO-8859-1,utf-8');
	}

	/**
     * Retrieve username
     *
     * @return string
     */
    public function getUsername() {
        return $this->username;
    }

    /**
     * Set username
     *
     * @param  string $value
     * @return Umpirsky_Service_Trim
     */
    public function setUsername($value) {
        $this->username = $value;
        $this->authInitialized = false;
        return $this;
    }

    /**
     * Retrieve password
     *
     * @return string
     */
    public function getPassword() {
        return $this->password;
    }

    /**
     * Set password
     *
     * @param  string $value
     * @return Umpirsky_Service_Trim
     */
    public function setPassword($value) {
        $this->password = $value;
        $this->authInitialized = false;
        return $this;
    }

	/**
     * Initialize HTTP authentication
     *
     * @return void
     */
    protected function init() {
    	if (null == $this->getUsername() || null == $this->getPassword()) {
    		return;
    	}

        $client = self::getHttpClient();
        $client->resetParameters();

        if (null == $this->cookieJar) {
            $client->setCookieJar();
            $this->cookieJar = $client->getCookieJar();
        } else {
            $client->setCookieJar($this->cookieJar);
        }

        if (!$this->authInitialized) {
            $client->setAuth($this->getUsername(), $this->getPassword());
            $this->authInitialized = true;
        }
    }

    /**
     * This is the main method for the tr.im API,
     * to submit a destination URL and get a unique tr.im URL.
     * You can send additional configuration options,
     * and attach it to an authenticated tr.im account.
     *
     * @param	string	$url
     * @return	Zend_Rest_Client_Result
     */
    public function trimUrl($url) {
    	$this->init();
        $path = '/api/trim_url.xml';
        $response = $this->restGet($path, array('url' => $url));
        return new Zend_Rest_Client_Result($response->getBody());
    }
}